const express = require('express');
const app = express();
const {balance} = require('./waterprice')
const {subsidy} = require('./subsidy')
//前端跨域
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});
//启动
app.get('/',(req,res)=>{
  res.send('启动成功');
})
//api
app.get('/water',async (req,res)=>{
  let id = req.query.id;
  console.log(id);
  let bal = await balance(id);
  console.log(bal);
  // let sub = await subsidy(id);
  // if (bal && sub) {
  if (bal) {
    res.json({
      state: 'OK',
      data:{
        id: req.query.id,
        balance: bal,
        
      },
      subsidy: 1    
    })
  }else{
    res.json({
      state: 'ERROR'  
    })
  }
})


app.listen(9002, function () {
  console.log("服务器启动成功");
  console.log('内网：', 'http://127.0.0.1:9002');
})